package com.betterjr.modules.risk.loan.service;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.betterjr.common.data.SimpleDataEntity;
import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.customer.ICustOpenAccountService;
import com.betterjr.modules.risk.loan.dao.LoanCustCodeRelationMapper;
import com.betterjr.modules.risk.loan.data.LoanCustCodeRelationConstantCollections;
import com.betterjr.modules.risk.loan.data.LoanProjectRelationConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanCustCodeRelation;
import com.google.common.collect.Lists;

/**
 * 
 * @ClassName: LoanCustCodeRelationService 
 * @Description: 核心企业与供应商代码绑定供应商信息 
 * @author xuyp
 * @date 2018年6月26日 下午5:15:37 
 *
 */
@Service
public class LoanCustCodeRelationService extends BaseService<LoanCustCodeRelationMapper, LoanCustCodeRelation> {

    @Reference(interfaceClass = ICustOpenAccountService.class)
    private ICustOpenAccountService accountService;

    /**
     * 
     * @Title: queryCodeRelation 
     * @Description:通过核心企业编号和供应商代码查询供应商编号
     * @param anCoreNo   核心企业编号
     * @param anCustCode 供应商代码
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 下午5:46:47
     */
    public List<LoanCustCodeRelation> queryCodeRelation(Long anCoreNo, String anCustCode) {

        Map map = QueryTermBuilder.newInstance().put("custCode", anCustCode).put("coreNo", anCoreNo).build();
        return this.selectByProperty(map, "priorityLeven desc");

    }

    /**
     * 
     * @Title: saveAddOrUpdateCodeRelation 
     * @Description: 更新或者新增供应商代码关系表
     * @param anCoreNo  核心企业编号
     * @param anCustCode  供应商代码
     * @param anCustNo  供应商编号
     * @param anFlag  标记  1 需要将优先级加1     2 不需要修改优先级
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月26日 下午5:45:21
     */
    @SuppressWarnings("unchecked")
    public LoanCustCodeRelation saveAddOrUpdateCodeRelation(Long anCoreNo, String anCustCode, Long anCustNo,
            String anFlag) {

        Map map = QueryTermBuilder.newInstance().put("custCode", anCustCode).put("coreNo", anCoreNo)
                .put("custNo", anCustNo).build();

        List<LoanCustCodeRelation> list = this.selectByProperty(map, "priorityLeven desc");

        LoanCustCodeRelation relation = new LoanCustCodeRelation();
        if (Collections3.isEmpty(list)) {

            relation.saveAddInitValue(anCustCode, anCoreNo, anCustNo);
            if (LoanCustCodeRelationConstantCollections.PRIORITYLEVEN_FLAG_INSERT.equals(anFlag)) {
                relation.setPriorityLeven(relation.getPriorityLeven() + 1);
            }

        } else {

            int priorityLeven = 0;

            relation = Collections3.getFirst(list);
            for (LoanCustCodeRelation loanCustCodeRelation : list) {
                priorityLeven += loanCustCodeRelation.getPriorityLeven();
                this.deleteByPrimaryKey(loanCustCodeRelation);
            }

            if (LoanCustCodeRelationConstantCollections.PRIORITYLEVEN_FLAG_INSERT.equals(anFlag)) {
                relation.setPriorityLeven(priorityLeven + 1);
            }

        }

        this.insertSelective(relation);
        return relation;

    }

    /**
     * 
     * @Title: queryAllCustList 
     * @Description: 查询所有的企业信 息 
     * @param coreNo  核心企业编号
     * @param custCode 供应商代码
     * @param exculeCustNos  返回值需要排除的企业编号
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月30日 上午11:01:55
     */
    @SuppressWarnings("unchecked")
    public List<SimpleDataEntity> queryAllCustList(Long coreNo, String custCode, String exculeCustNos) {

        List<SimpleDataEntity> infos = accountService.queryAllCustInfo();
        // BTAssert.isTrue(!Collections3.isEmpty(infos), "查询数据为空");
        if (Collections3.isEmpty(infos) || (coreNo == null && StringUtils.isBlank(custCode))) return infos;

        Map<String, Object> map = QueryTermBuilder.newInstance().put("coreNo", coreNo).put("custCode", custCode)
                .build();

        List<LoanCustCodeRelation> list = this.selectByProperty(map, "priorityLeven desc");
        StringBuilder sb = new StringBuilder();
        for (LoanCustCodeRelation loanCustCodeRelation : list) {
            sb.append(loanCustCodeRelation.getCustNo())
                    .append(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR);
        }

        List<SimpleDataEntity> sortCustInfos = sortSimpleEntity(infos, sb.toString(), exculeCustNos);

        return sortCustInfos;
    }

    /**
     * 
     * @Title: sortSimpleEntity 
     * @Description: 排序供应商编号
     * @param infos  所有的数据集
     * @param sortCustNos  排序的custNos  custNo,custNo,custNo
     * @param exculeCustNos 排除的custNos
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月30日 上午11:20:12
     */
    private List<SimpleDataEntity> sortSimpleEntity(List<SimpleDataEntity> infos, String sortCustNos,
            String exculeCustNos) {

        if (Collections3.isEmpty(infos)) return infos;

        LinkedList<SimpleDataEntity> resultList = new LinkedList<SimpleDataEntity>();
        LinkedList<String> sortCustNoList = new LinkedList<String>();
        LinkedList<String> exculeCustNoList = new LinkedList<String>();

        if (StringUtils.isNoneBlank(sortCustNos)) {
            String[] sortCustNosArray = sortCustNos
                    .split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR);
            sortCustNoList.addAll(Arrays.asList(sortCustNosArray));
        }

        if (StringUtils.isNoneBlank(exculeCustNos)) {
            String[] exculeCustNosArray = exculeCustNos
                    .split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR);
            exculeCustNoList.addAll(Arrays.asList(exculeCustNosArray));
        }

        // 删除取消的编号
        LinkedList<SimpleDataEntity> exculeList = new LinkedList<SimpleDataEntity>();

        for (String exculeCustNo : exculeCustNoList) {

            if (sortCustNoList.contains(exculeCustNo)) sortCustNoList.remove(exculeCustNo);

            for (SimpleDataEntity simple : infos) {
                if (simple.getValue().equals(exculeCustNo)) exculeList.add(simple);
            }

        }

        infos.removeAll(exculeList);

        // 开始排序
        exculeList.clear();
        for (String soreCustNo : sortCustNoList) {

            for (SimpleDataEntity simple : infos) {
                if (simple.getValue().equals(soreCustNo)) {
                    resultList.add(simple);
                    exculeList.add(simple);
                }
            }

        }

        // 收集结果
        infos.removeAll(exculeList);
        resultList.addAll(infos);
        return resultList;
    }

}
